//
//  StatusOriginalView.swift
//  上海微博
//
//  Created by mac on 16/5/18.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

/// 微博头像尺寸
private let CZStatusIconSize = CGSize(width: 36, height: 36)
/// 原创微博
class StatusOriginalView: UIView {

    
    //微博视图模型
    var statusModel :StatusViewModel?{
        didSet{
            //didSet 用来监听除了初始化之外的属性值的变化 已经变化
            
            //获取最终数据
            print(statusModel?.status.user)
            
            //设置控件数据
            //头像 是网路图像 不能使用之前的本地设置图像
//            iconView.cz_setImageWithURL(statusModel?.status.user?.profile_image_url, placeholderName: "avatar_default_big")
            iconView.cz_setImageWithURL(statusModel?.status.user?.profile_image_url,
                                        placeholderName: "avatar_default_big",
                                        size: CZStatusIconSize, isCorner: true)
            
            //姓名
            nameLabel.text=statusModel?.status.user?.screen_name
            //判断会员等级
            memberIconView.image=statusModel?.userMemberImage
            //判断vip
            vipIconView.image=statusModel?.userVipImage
            //TODO : 设置
            //时间
            timeLabel.text=statusModel?.creatTime?.cz_dateDescription
            //来源
            sourceLabel.text=statusModel?.sourceString
            //文字
            contentLabel.text=statusModel?.status.text
            
            //根据是否有配图 决定顶部约束的参照
            let offset = statusModel?.status.pic_urls?.count>0 ? cz_statusLayout.margin:0
            
            //'更新' 配图视图大小
            pictureView.snp_updateConstraints { (make) in
                make.size.equalTo(statusModel!.pictureSizeView)
                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
            }
            
            //设置图片视图
            pictureView.pic_url=statusModel?.status.pic_urls
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor=UIColor.whiteColor()
        SetupUI()
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 私有控件
    /// 头像图标
    private let iconView: UIImageView = UIImageView(cz_imageName: "avatar_default_big")
    /// 姓名标签
    private let nameLabel: UILabel = UILabel(cz_text: "传智播客")
    /// 会员图标
    private let memberIconView: UIImageView = UIImageView(cz_imageName: "common_icon_membership_level1")
    /// VIP 图标
    private let vipIconView: UIImageView = UIImageView(cz_imageName: "avatar_vip")
    /// 时间
    private let timeLabel: UILabel = UILabel(cz_text: "创建时间", fontSize: 10, color: CZAppearanceTintColor)
    /// 来源
    private let sourceLabel: UILabel = UILabel(cz_text: "来源 上海微博", fontSize: 10)
    /// 内容标签
    private let contentLabel: UILabel = UILabel(cz_text: "微博微博", fontSize: 15)
    // 图片视图
    private let pictureView: StatusPicture = StatusPicture()
}
///设置原创微博控件
extension StatusOriginalView {
    func SetupUI() {
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 测试数据
        // 测试代码
        let count = random() % 20 + 1
        var str = ""
        for _ in 0..<count {
            str += "新浪微博"
        }
        //
        contentLabel.text = str
        //自动布局
        //间距
        let margin: CGFloat = 12.0
        
        //设置头像
        UIImage(named: "avatar_default_big")?.cz_asyncDrawImage(CZStatusIconSize, isCorner: true, finished: { (image) in
            self.iconView.image=image
        })
        //头像
        iconView.snp_makeConstraints { (make) in
            make.left.equalTo(margin)
            make.top.equalTo(margin)
            make.size.equalTo(CZStatusIconSize)
        }
        //姓名标签
        nameLabel.snp_makeConstraints { (make) in
            make.top.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        //会员图标
        memberIconView.snp_makeConstraints { (make) in
            make.top.equalTo(nameLabel)
            make.left.equalTo(nameLabel.snp_right).offset(margin)
        }
        //VIP 图标
        vipIconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView.snp_right).offset(-4)
            make.centerY.equalTo(iconView.snp_bottom).offset(-4)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(iconView)
            make.left.equalTo(iconView.snp_right).offset(margin)
        }
        //来源
        sourceLabel.snp_makeConstraints { (make) in
            make.bottom.equalTo(timeLabel)
            make.left.equalTo(timeLabel.snp_right).offset(margin)
        }
        //内容标签
        contentLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            
            make.right.equalTo(self).offset(-margin)
            
        }
        /// 图片视图
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            //make.right.equalTo(self).offset(-margin)
            make.top.equalTo(contentLabel.snp_bottom).offset(margin)
            
            // 默认不设置配图视图大小
            make.size.equalTo(CGSizeZero)
            
            // 很重要，设置底部约束，能够自动计算高度
            make.bottom.equalTo(self).offset(-margin)
        }
    }
}